//
//  LoginVC.m
//  leyitui
//
//  Created by 王大江 on 17/9/18.
//  Copyright © 2017年 xinleyi. All rights reserved.
//

#import "LoginVC.h"
#import "PGTabBarController.h"
#import "NSString+Check.h"
#import "Loger3DNewUserRegisterVC.h"

@interface LoginVC ()<UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *usernameTF;
@property (weak, nonatomic) IBOutlet UITextField *passwdTF;



@end

@implementation LoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //状态栏白色
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
    
//    // 设置导航控制器的代理为self
//    self.navigationController.delegate = self;
    //利用textfield的leftView属性
//    UIImageView *imageused=[[UIImageView alloc]initWithFrame:CGRectMake(-20, self.usernameTF.frame.size.height/2.0f-47/4.0f, 37/2.0f, 47/2.0f)];
//    imageused.image=[UIImage imageNamed:@"login_username"];
//    self.usernameTF.leftView = imageused;
//    self.usernameTF.leftViewMode = UITextFieldViewModeAlways;
    
    [self setNavigationStyle];
    
    
}

#pragma 设置导航栏样式 - 去背景、去底部线条、状态栏白色
- (void)setNavigationStyle {
    //去掉背景图片
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    //去掉底部线条
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
}

#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}

#pragma mark 利用touchbegin事件实现点空白处收起键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.usernameTF resignFirstResponder];
    [self.passwdTF resignFirstResponder];
}

- (IBAction)loginBtnClick:(id)sender {
    if (![self.usernameTF.text checkPhoneNumInput]) {
        [MBProgressHUD showError:@"请输入正确的手机号"];
        return;
    }
    
    NSMutableDictionary *para = [[NSMutableDictionary alloc] init];
    para[@"mobile"] = self.usernameTF.text;
    para[@"password"] = self.passwdTF.text;
    
    [XYNetworking POST:@"api/login" baseURL:LYT_BASE_URL params:para success:^(NSURLSessionDataTask *task, id responseObject) {
        NSDictionary *dict = (NSDictionary*)responseObject;
        if ([dict[@"code"] intValue] == 0) {
            //登陆成功,存下token到沙盒,跳转到主页
            [[NSUserDefaults standardUserDefaults] setObject:dict[@"token"] forKey:@"token"];
            [self restoreRootViewController:[[PGTabBarController alloc] init]];
            
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [MBProgressHUD showError:@"请检查网络"];
    }];
    
}

// 登陆后淡入淡出更换rootViewController
- (void)restoreRootViewController:(UIViewController *)rootViewController
{
    typedef void (^Animation)(void);
    UIWindow* window = [UIApplication sharedApplication].keyWindow;
    
    rootViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    Animation animation = ^{
        BOOL oldState = [UIView areAnimationsEnabled];
        [UIView setAnimationsEnabled:NO];
        [UIApplication sharedApplication].keyWindow.rootViewController = rootViewController;
        [UIView setAnimationsEnabled:oldState];
    };
    
    [UIView transitionWithView:window
                      duration:0.5f
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:animation
                    completion:nil];
    
}

- (IBAction)newUserRegisterBtn:(id)sender {
    Loger3DNewUserRegisterVC *regist = [[Loger3DNewUserRegisterVC alloc]init];
//    regist.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:regist animated:YES];
    
}

- (IBAction)forgetPassWdBtn:(id)sender {
    
    
}

- (IBAction)otherLoginWechatBtn:(id)sender {
    
}

- (IBAction)otherLoginQQBtn:(id)sender {
    
    
}




@end
